<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
</head>

<body>
    <nav th:fragment="header" class="navbar navbar-expand-lg navbar-light fixed-top bg-white shadow-sm">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-utensils text-primary me-2"></i>
                美食社区
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/}"
                            th:classappend="${#httpServletRequest.requestURI == '/' ? 'active' : ''}">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/category}"
                            th:classappend="${#httpServletRequest.requestURI == '/category' ? 'active' : ''}">美食分类</a>
                    </li>
                    <!-- <li class="nav-item">
                        <a class="nav-link" th:href="@{/restaurant}"
                            th:classappend="${#httpServletRequest.requestURI == '/restaurant' ? 'active' : ''}">探索餐厅</a>
                    </li> -->
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/restaurant/map}"
                            th:classappend="${#httpServletRequest.requestURI == '/restaurant/map' ? 'active' : ''}">餐厅地图</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" th:href="@{/posts}"
                            th:classappend="${#httpServletRequest.requestURI == '/posts' ? 'active' : ''}">美食日记</a>
                    </li>
                    <li class="nav-item" th:if="${session.user != null && session.user.role != null && session.user.role == 'admin'}">
                        <a class="nav-link" th:href="@{/analysis}"
                            th:classappend="${#httpServletRequest.requestURI == '/analysis' ? 'active' : ''}">数据分析</a>
                    </li>
                    <li class="nav-item" th:if="${session.user != null && session.user.role != null && session.user.role == 'admin'}">
                        <a class="nav-link" th:href="@{/admin/users}"
                            th:classappend="${#httpServletRequest.requestURI == '/admin/users' ? 'active' : ''}">管理用户</a>
                    </li>
                    <li class="nav-item" th:if="${session.user != null && session.user.role != null && session.user.role == 'admin'}">
                        <a class="nav-link" th:href="@{/admin/posts}"
                            th:classappend="${#httpServletRequest.requestURI == '/admin/posts' ? 'active' : ''}">管理美食日记</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown" th:if="${session.user != null}">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                            data-bs-toggle="dropdown" aria-expanded="false">
                            <img th:src="${session.user.avatar != null ? session.user.avatar : '/images/default-avatar.jpg'}"
                                alt="用户头像" class="rounded-circle me-2"
                                style="width: 24px; height: 24px; object-fit: cover;">
                            <span th:text="${session.user.nickname}">用户</span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                            <li th:if="${session.user != null && session.user.role != null && session.user.role == 'admin'}"><a class="dropdown-item" th:href="@{/admin/users}"><i
                                        class="fas fa-users-cog me-2"></i>管理用户</a></li>
                            <li th:if="${session.user != null && session.user.role != null && session.user.role == 'admin'}"><a class="dropdown-item" th:href="@{/admin/posts}"><i
                                        class="fas fa-utensils me-2"></i>管理美食日记</a></li>
                            <li><a class="dropdown-item" th:href="@{/user/profile}"><i
                                        class="fas fa-user me-2"></i>个人资料</a></li>
                            <!--<li><a class="dropdown-item" th:href="@{/user/posts}"><i-->
                            <!--            class="fas fa-book me-2"></i>我的文章</a></li>-->
                            <li>
                                <hr class="dropdown-divider">
                            </li>
                            <li><a class="dropdown-item" href="javascript:void(0);" onclick="logout()"><i
                                        class="fas fa-sign-out-alt me-2"></i>退出登录</a></li>
                        </ul>
                    </li>
                    <li class="nav-item" th:if="${session.user == null}">
                        <a class="nav-link btn btn-outline-primary me-2" href="/login">登录</a>
                    </li>
                    <li class="nav-item" th:if="${session.user == null}">
                        <a class="nav-link btn btn-primary" th:href="@{/register}">注册</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <script th:fragment="logout-script">
        function logout() {
            $.post('/api/user/logout', function () {
                window.location.href = '/login';
            });
        }
    </script>
</body>

</html>